﻿using System;
using AgencyEntityLib.BaseEntity;
using Grishko.DataTier;
using Grishko.SerilizeTools;
using OutdoorLib.Context;
using System.Linq;
using OutdoorLib.BaseEntity.Dictionary;
using OutdoorLib.BaseEntity.Agency;

namespace OutdoorLib.BaseEntity.Constraction
{
    /// <summary>
    /// Рекламный носитель    
    /// </summary>
    [TagNameInfo(TagName = "Constraction")]
    public class ConstractionEntity : SqlAgencyEntity
    {
        
        private TreeDictionaryItemValue _ConstractionType = null;
        private TreeDictionaryItemValue _AdressProgramm = null;        
        private TreeDictionaryItemValue _RestrictionsOnPlacement= null;
        private TreeDictionaryItemValue _GeoBinding = null;
        private TreeDictionaryItemValue _LocationDescribe = null;
        private DateTime _dtCreate = DateTime.Now;
        private DateTime _dtDestroy = AgencyLib_GlobalConstants.MaxDate;
        private SideList _SideList = new SideList();

        /// <summary>
        /// Тип конструкции - 
        /// описывается как связанный список элементов (тип конструкции - тип - значение)
        /// </summary>            
        
        
        [SrchItemInfo(TxtPropertyName = "Тип носителя", TreeDictionaryTypeID =TreeDictionaryItem .TreeDictionary_ConstractionType, PropControlType = SrchItemInfo.PropertyControlType.Tree)]        
        public TreeDictionaryItemValue  ConstractionType
        {
            get
            {
                return _ConstractionType;
            }
            set
            {
                _ConstractionType = value;                
            }        
        }

        
        [EntityNonSerialized]
        public string ConstractionTypeAsString
        {
            get
            {
                if (null == ConstractionType)
                    return string.Empty;

                return ConstractionType.ToString();
            }
        }

        [SrchItemInfo(TxtPropertyName = "Призма")]        
        public bool IsPrizmaVision 
        {
            get 
            {
                var soldOutProducts =
                from p in this.SideList.ToArray<Side >()  
                where p.SurfaceCount  > 1
                select p;

                return soldOutProducts.ToArray<Side>().Length > 0;   
    
            }
        }


        [SrchItemInfo(TxtPropertyName = "Ограничения размещения", TreeDictionaryTypeID = TreeDictionaryItem.TreeDictionary_RestrictionsOnPlacement, PropControlType = SrchItemInfo.PropertyControlType.Tree)]
        public TreeDictionaryItemValue RestrictionsOnPlacement
        {
            get
            {
                return _RestrictionsOnPlacement;
            }
            set
            {
                _RestrictionsOnPlacement = value;
            }
        }
        
        [EntityNonSerialized ]
        public string RestrictionsOnPlacementAsString
        {
            get
            {
                if (null == RestrictionsOnPlacement)
                    return string.Empty;

                return RestrictionsOnPlacement.ToString();
            }
        }

        [SrchItemInfo(TxtPropertyName = "Расположение",  TreeDictionaryTypeID =TreeDictionaryItem .TreeDictionary_LocationDescribe, PropControlType = SrchItemInfo.PropertyControlType.Tree)]
        public TreeDictionaryItemValue LocationDescribe
        {
            get
            {
                return _LocationDescribe;
            }
            set
            {
                _LocationDescribe = value;
            }
        }
      
        public string LocationDescribeAsString
        {
            get
            {
                if (null == LocationDescribe)
                    return string.Empty;

                return LocationDescribe.ToString();
            }
        }
                

        [SrchItemInfo(TxtPropertyName = "Адресная программа", TreeDictionaryTypeID=TreeDictionaryItem .TreeDictionary_AdressProgram, PropControlType = SrchItemInfo.PropertyControlType.Tree)]        
        public TreeDictionaryItemValue AdressProgramm
        {
            get
            {                
                return _AdressProgramm ;
            }
            set
            {
                _AdressProgramm = value;
            }
        }

        [EntityNonSerialized]
        public string AdressProgrammAsString
        {
            get
            {
                if (null == AdressProgramm)
                    return string.Empty;

                return AdressProgramm.ToString();
            }
        }


        [SrchItemInfo(TxtPropertyName = "Привязка", TreeDictionaryTypeID =TreeDictionaryItem .TreeDictionary_GeoBinding , PropControlType = SrchItemInfo.PropertyControlType.Tree)]        
        public TreeDictionaryItemValue GeoBinding
        {
            get
            {

                return _GeoBinding;
            }
            set
            {
                _GeoBinding = value;
            }
        }

        [EntityNonSerialized]
        public string GeoBindingAsString
        {
            get
            {
                if (null == AdressProgramm)
                    return string.Empty;

                return AdressProgramm.ToString();
            }
        }
                

        [SrchItemInfo(TxtPropertyName = "Улица")]        
        public string Street{get; set ;}       
        
        [SrchItemInfo(TxtPropertyName = "Номер")]        
        public string Number{ get; set; }

        
        /// <summary>
        /// Датат монтажа конструкции
        /// </summary>
        [SrchItemInfo(TxtPropertyName = "Дата монтажа")]        
        public DateTime dtCreate { get { return _dtCreate; } set { _dtCreate = value; } }
      
        public string dtCreateAsString 
        {
            get { return dtCreate.ToShortDateString(); }
        } 
        
        /// <summary>
        /// Дата демонтажа 
        /// </summary>
        [SrchItemInfo(TxtPropertyName = "Дата демонтажа")]                
        public DateTime dtDestroy { get { return _dtDestroy; } set { _dtDestroy = value; } }

        public string dtDestroyAsString
        {
            get { return dtDestroy.ToShortDateString(); }
        } 

        [SrchItemInfo(TxtPropertyName = "Подсветка")]        
        public bool Light
        {
            get;
            set;
        }
        

        
        /// <summary>
        /// стороны носителя
        /// </summary>  
        [SrchItemInfo(TxtPropertyName = "Стороны", ListType = typeof(Side))]
        public SideList SideList { get { return _SideList; } set { _SideList = value; } }
        
        private double  _MapX = 0;
        [SrchItemInfo(TxtPropertyName = "MapX")]        
        public double   MapX { get { return _MapX; } set { _MapX = value; } }

        private Double  _MapY = 0;
        [SrchItemInfo(TxtPropertyName = "MapY")]        
        public double MapY { get { return _MapY; } set { _MapY = value; } }

        
        [SrchItemInfo(TxtPropertyName = "Описание")] 
        public string Describe { get;set;}

        [EntityNonSerialized]
        public string FullAdress 
        {
            get { return AdressProgrammAsString +" "+ (String.IsNullOrEmpty (Street)?"":Street);  }
        }

        
        
    }

    public class SideList:EntityList 
    {
        ///// Диапазон литер для стороны        
        //string[] Liters = { "A", "B", "C", "D", "E", "F" };
        //public override void Add(EntitySingle  obj)
        //{
        //    if(this.Count>Liters.Length )
        //        throw new Exception (string.Format ("Количество сторон не может превышать {0} ",Liters.Length));

        //    (obj as Side).Litera = Liters[this.Count];
        //    base.Add(obj);
        //}

    }


}
